<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"
           @keyup.enter.native="submitForm('ruleForm')">

    <el-form-item label="手机号" prop="phone" :rules="rules.phone">
      <el-input type="text" v-model.number.trim="ruleForm.phone"
                clearable
                maxlength="11"
                show-word-limit>
      </el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pass" :rules="rules.pass">
      <el-input type="password" v-model="ruleForm.pass" autocomplete="off">
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        phone: '',
        pass: '',
        checkPass: '',
      },
      rules: {
        phone: [
          {required: true, message: "手机号不能为空"},
          {type: "number", message: "手机号码必须为数字"},
          {
            pattern:
                /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: "请填写正确的手机号码",
            trigger: "blur",
          },
        ],
        pass: [
          {required: true, message: "手机号不能为空"},
          {
            pattern:
                /^\w{8,16}$/,
            message: "请填写正确的密码格式",
            trigger: "blur",
          },
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$notify({
            title: '成功',
            message: '登录成功',
            type: 'success'
          });

          //置空
          this.ruleForm.pass = ''
          this.ruleForm.phone = ''
          //跳转
          this.$router.push('music')
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style scoped>
.demo-ruleForm {
  margin: 0 auto;
  max-width: 35vw;
}
</style>